﻿using System;
using System.Collections.Generic;
using System.Text;

namespace EvePoint.Library.HtmlControls
{
    /// <summary>
    /// Allows programmatic access to the Mobile WebKit specific meta tag: viewport
    /// </summary>

    public class Viewport: System.Web.UI.HtmlControls.HtmlMeta, EvePoint.Library.HtmlControls.IViewport
    {

        #region Constants

        /// <summary>
        /// Minimal possible viewport width setting
        /// </summary>
        public static readonly int MINVIEWPORTWIDTH = 200;

        /// <summary>
        /// Maximum possible viewport width setting
        /// </summary>
        public static readonly int MAXVIEWPORTWIDTH=10000;

       /// <summary>
        /// Minimal possible viewport height setting
       /// </summary>
        public static readonly int MINVIEWPORTWHEIGHT = 223;
        /// <summary>
        /// Maximum possible viewport height setting
        /// </summary>
        public static readonly int MAXVIEWPORTHEIGHT = 10000;

        /// <summary>
        /// Minimum possible scale factor
        /// </summary>
        public static readonly decimal MINSCALE = 0m;

        /// <summary>
        /// Maximum possible scale factor
        /// </summary>
        public static readonly decimal MAXSCALE = 10m;


        public static readonly string VIEWPORTCOMPATKEY = "viewportMetaCompatible";
        
   


        #endregion


        #region Properties

        public override string Name
        {
            get
            {
                return "viewport";
            }
           
        }


        /// <summary>
        /// Determines whether a user can resize the WebPage using gestures.
        /// </summary>
        public bool? UserScalable
        {
            get
            {
                if (ViewState["UserScalable"] == null)
                {
                    return null;
                }
                else
                {
                    return (bool?)ViewState["UserScalable"]; 
                }
            }
            set
            {
                ViewState["UserScalable"] = value;
            }

        }

        /// <summary>
        /// If true, use Device Height and Width.
        /// For native mobile WebKit applications.
        /// </summary>
        public bool? UseDeviceHeightAndWidth
        {
            get
            {
                if (ViewState["UseDeviceHeightAndWidth"] == null)
                {
                    return null;
                }
                else
                {
                    return (bool?)ViewState["UseDeviceHeightAndWidth"];
                }
            }
            set
            {
                ViewState["UseDeviceHeightAndWidth"] = value;
            }

        }

        /// <summary>
        /// The scale on Page Load before any User Interaction.
        /// </summary>
        public decimal? InitialScale
        {
            get
            {
                if (ViewState["InitialScale"] == null)
                {
                    return null;
                }
                else
                {
                    return (decimal?)ViewState["InitialScale"];
                }
            }
            set
            {
                if (value.HasValue)
                {
                    if (value.Value <= MINSCALE)
                    {
                        throw new ArgumentOutOfRangeException("InitialScale is set to an invalid scale."); 
                    }
                    else if (value.Value > MAXSCALE)
                    {
                        throw new ArgumentOutOfRangeException("InitialScale is set to an invalid scale."); 
                    }

                }

                ViewState["InitialScale"] = value;
            }
        }

        /// <summary>
        /// The minimum scale a web page can be resized to.
        /// </summary>
        public decimal? MinimumScale
        {
            get
            {
                if (ViewState["MinimumScale"] == null)
                {
                    return null;
                }
                else
                {
                    return (decimal?)ViewState["MinimumScale"];
                }
            }
            set
            {
                if (value.HasValue)
                {
                    if (value.Value <= MINSCALE)
                    {
                        throw new ArgumentOutOfRangeException("MinimumScale is set to an invalid scale.");
                    }
                    else if (value.Value > MAXSCALE)
                    {
                        throw new ArgumentOutOfRangeException("MinimumScale is set to an invalid scale.");
                    }

                }


                ViewState["MinimumScale"] = value;
            }
        }

        /// <summary>
        /// The maximum scale a web page can be resized to.
        /// </summary>
        public decimal? MaximumScale 
        {
            get
            {
                if (ViewState["MaximumScale"] == null)
                {
                    return null;
                }
                else
                {
                    return (decimal?)ViewState["MaximumScale"];
                }
            }
            set
            {
                if (value.HasValue)
                {
                    if (value.Value <= MINSCALE)
                    {
                        throw new ArgumentOutOfRangeException("MaximumScale is set to an invalid scale.");
                    }
                    else if (value.Value > MAXSCALE)
                    {
                        throw new ArgumentOutOfRangeException("MaximumScale is set to an invalid scale.");
                    }

                }

                ViewState["MaximumScale"] = value;
            } 
        }

        /// <summary>
        /// Width of Perspective 
        /// </summary>
        public int? Width
        {
            get
            {
                if (ViewState["Width"] == null)
                {
                    return null;
                }
                else
                {
                    return (int?)ViewState["Width"];
                }
            }
            set
            {

                if (value.HasValue)
                {
                    if (value.Value <= MINVIEWPORTWIDTH)
                    {
                        throw new ArgumentOutOfRangeException("Width is set to an invalid amount.");
                    }
                    else if (value.Value > MAXVIEWPORTWIDTH)
                    {
                        throw new ArgumentOutOfRangeException("Width is set to an invalid amount.");
                    }

                }


                ViewState["Width"] = value;
            } 
        }

        /// <summary>
        /// Height of Perspective
        /// </summary>
        public int? Height
        {
            get
            {
                if (ViewState["Height"] == null)
                {
                    return null;
                }
                else
                {
                    return (int?)ViewState["Height"];
                }
            }
            set
            {
                if (value.HasValue)
                {
                    if (value.Value <=  MINVIEWPORTWHEIGHT)
                    {
                        throw new ArgumentOutOfRangeException("Height is set to an invalid amount.");
                    }
                    else if (value.Value > MAXVIEWPORTHEIGHT)
                    {
                        throw new ArgumentOutOfRangeException("Height is set to an invalid amount.");
                    }

                }


                ViewState["Height"] = value;
            } 
        }

        #endregion 

        #region Methods

        public Viewport()
        {

         

        }

        protected override void CreateChildControls()
        {
            if (Page.Request.Browser[VIEWPORTCOMPATKEY] != null)
            {
                if (Page.Request.Browser[VIEWPORTCOMPATKEY].ToLower().Equals("true"))
                {
                    SetViewportContent();
                }
                else
                {
                    Visible = false; 
                }
            }
            else
            {
                Visible = false;
            }

            base.CreateChildControls();

        }

        private void SetViewportContent()
        {
            StringBuilder viewPortBuilder =new StringBuilder();

            if (UserScalable.HasValue && UserScalable.Value)
            {
                viewPortBuilder.Append("user-scalable=1;");
            }
            else if(UserScalable.HasValue) 
            {
                viewPortBuilder.Append("user-scalable=0;");
            }


            if (UseDeviceHeightAndWidth.HasValue && UseDeviceHeightAndWidth.Value)
            {
                viewPortBuilder.Append("height=device-height;");
                viewPortBuilder.Append("width=device-width;"); 
            }
            else
            {

                if (Width.HasValue)
                {
                    viewPortBuilder.AppendFormat("width={0};", Width);
                }

                if (Height.HasValue)
                {
                    viewPortBuilder.AppendFormat("height={0};", Height);
                }
            }

            if (InitialScale.HasValue)
            {
                viewPortBuilder.AppendFormat("initial-scale={0};", InitialScale);
            }

            if (MinimumScale.HasValue)
            {

                viewPortBuilder.AppendFormat("minimum-scale={0};", MinimumScale);
            }

            if (MaximumScale.HasValue)
            {
                viewPortBuilder.AppendFormat("maximum-scale={0};", MaximumScale); 
            }


            Content = viewPortBuilder.ToString();

            
        }

        #endregion 

    }
}
